package com.mengtian_tech.tju.dataquery.api.controller;

import java.math.BigInteger;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mengtian_tech.tju.dataquery.api.service.AskLeaveService;
import com.mengtian_tech.tju.dataquery.api.service.CarDataHandleService;
import com.mengtian_tech.tju.dataquery.api.service.CarParkService;
import com.mengtian_tech.tju.dataquery.api.service.CarVisitApplyService;
import com.mengtian_tech.tju.dataquery.api.service.EmployeeService;
import com.mengtian_tech.tju.dataquery.api.service.GateDataHandleService;
import com.mengtian_tech.tju.dataquery.api.service.LoginSessionService;
import com.mengtian_tech.tju.dataquery.api.service.PersonVisitApplyService;
import com.mengtian_tech.tju.dataquery.api.service.PersonVisitLogService;
import com.mengtian_tech.tju.dataquery.api.task.executors.HandleCollegeIdExecutor;
import com.mengtian_tech.tju.dataquery.api.task.executors.HandleCollegeIdForPersonExecutor;
import com.mengtian_tech.tju.dataquery.api.util.ResponseUtil;
import com.mengtian_tech.tju.dataquery.api.util.SpringUtil;


@RestController
@RequestMapping("/api/emp")
public class TestApiController {
	
	
	@Autowired
	EmployeeService employeeService;
	
	@Autowired
	LoginSessionService loginSessionService;
	
	@Autowired
	PersonVisitLogService personVisitLogService;
	
	@Autowired
	PersonVisitApplyService personVisitApplyService;
	
	@Autowired
	CarVisitApplyService carVisitApplyService;
	
	@Autowired
	CarParkService carParkService;
	
	@Autowired
	AskLeaveService askLeaveService;
	
	@Autowired
	GateDataHandleService gateDataHandleService;
	
	@Autowired
	CarDataHandleService carDataHandleService;
	

	/**
	 * 获取审批意见
	 * @param formId
	 * @return
	 */
	@RequestMapping("ishead")
	public Object ishead(String userId) {;
		System.out.println(this.employeeService.findById("116068"));
		return ResponseUtil.ok();
	}


	@RequestMapping("personVisitLogService")
	public Object personVisitLogService(String userId) {;
		personVisitLogService.aysncPersonVisitLog();
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("PersonVisitApplyService")
	public Object personVisitApplyService() {
		personVisitApplyService.aysncPersonVisitApply();
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("CarVisitApplyService")
	public Object CarVisitApplyService() {
		this.carVisitApplyService.asyncBusApply();
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("asyncSchoolCarApply")
	public Object asyncSchoolCarApply() {
		this.carVisitApplyService.asyncSchoolCarApply();
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("asyncStudentCarApply")
	public Object asyncStudentCarApply() {
		this.carVisitApplyService.asyncStudentCarApply();
		return ResponseUtil.ok("ok");
	}
	

	@RequestMapping("asyncTeacherApply")
	public Object asyncTeacherApply() {
		this.carVisitApplyService.asyncTeacherApply();
		return ResponseUtil.ok("ok");
	}


	@RequestMapping("asyncCarPark")
	public Object asyncCarPark() {
		this.carParkService.asyncCarPark();
		return ResponseUtil.ok("ok");
	}
	

	@RequestMapping("askLeaveService")
	public Object askLeaveService() {
		try {
			this.askLeaveService.asyncLeave();
			
			
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("gateDataHandleService")
	public Object gateDataHandleService() {
		try {
			int count = this.gateDataHandleService.handle();
			while(count > 0) {
				count = this.gateDataHandleService.handle();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("rptLeaveData")
	public Object rptLeaveData() {
		try {
			int count = this.askLeaveService.dealData();
			while(count > 0) {
				count = this.askLeaveService.dealData();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("carDataHandleService")
	public Object carDataHandleService() {
		try {
			int count = this.carDataHandleService.handle();
			while(count > 0) {
				count = this.carDataHandleService.handle();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	
	@RequestMapping("shutdownapplication-")
	public Object shutdownapplication() {
		SpringApplication.exit(SpringUtil.getContext());
		return ResponseUtil.ok("ok");
	}
	
	
	@RequestMapping("userGateRpt")
	public Object userGateRpt() {
		try {
			BigInteger lastId =  this.gateDataHandleService.handleRpt(null);
			while( lastId != null) {
				 lastId = this.gateDataHandleService.handleRpt(lastId);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("carDataRpt")
	public Object carDataRpt() {
		try {
			Long lastId =  this.carDataHandleService.handleRpt(null);
			while(lastId != null) {
				lastId = this.carDataHandleService.handleRpt(lastId);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("carDataUserType")
	public Object handleUserType() {
		try {
			Long lastId =  this.carDataHandleService.handleUserType(null);
			while(lastId != null) {
				lastId = this.carDataHandleService.handleUserType(lastId);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("handleCollegeIdForCarPark")
	public Object handleCollegeIdForCarPark() {

		for(int i=0;i<4;i++) {//启动四个线程更新
			Thread thread = new Thread(new HandleCollegeIdExecutor());
			thread.start();
		}
		
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("handleCollegeIdForPerson")
	public Object handleCollegeIdForPerson() {

		for(int i=0;i<4;i++) {//启动四个线程更新
			Thread thread = new Thread(new HandleCollegeIdForPersonExecutor());
			thread.start();
		}
		
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("initOUtTime")
	public Object initOutTime() {
		System.out.println("begin init out time "+ new Date());
		try {
			Long lastId = this.carParkService.doAsyncOutTime(null);		
			
			while(lastId != null) {
				lastId = this.carParkService.doAsyncOutTime(lastId);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("end init out time "+ new Date());
		return ResponseUtil.ok("ok");
	}
	
	@RequestMapping("testInitOutTimeformTemp")
	public Object doAsyncOutTimeFromTempTable() {
		System.out.println("begin init out time "+ new Date());
		try {
			Long lastId = this.carParkService.doAsyncOutTimeFromTempTable(null);		
			
			while(lastId != null) {
				lastId = this.carParkService.doAsyncOutTimeFromTempTable(lastId);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("end init out time "+ new Date());
		return ResponseUtil.ok("ok");
	}
	
}
